git worktree の leaf の中でルートの .git までの path を取得する

git - How to find the main worktree from any worktree - Stack Overflow

$ git rev-parse --path-format=absolute --git-common-dir

/path/to/repository/.git

後は .git を除けば OK そう。

$ git rev-parse --path-format=absolute --git-common-dir | sed -e 's/.git//g'

背景

以下のようなシェルスクリプトを使っていたが、

function gwt() {
    GIT_CDUP_DIR=`git rev-parse --show-cdup`
    git worktree add ${GIT_CDUP_DIR}git-worktrees/$1 -b $1
}

これを worktree の中で実行するとルートではなく、 leaf の .git までの相対パスになってしまうので、使い勝手が悪かった。
上記の方法で GIT_CDUP_DIR を取得すればルートが分かるので、解決した。

Git git-worktree